<?php
/**
 * Created by PhpStorm.
 * User: 50223
 * Date: 18.7.25
 * Time: 16:16
 */
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Session;
use Illuminate\Support\Facades\DB;

class LoginController extends Controller
{


    public function login(Request $request){

        if($request->isMethod('POST')){
            $userInput = $request->input('User');
            // 定义验证规则
            $validator = \Validator::make($request->input(),[
                'User.username'=>'required|min:2|max:20',
                'User.password'=>'required|alpha_dash',

            ],[
                'required'=>':attribute 为必填项',
                'min'=>':attribute 长度不符合要求',
                'alpha_dash'=>':attribute 只能是字母、数字、破折号（ - ）以及下划线（ _ ）'
            ],[
                'User.username'=>'账号',
                'User.password'=>'密码',

            ]);
            if($validator->fails()){
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $username = $userInput['username'];

            $res = DB::table('users')->where('username','=',$username)->select('password','id')->first();
//            先判断账号是否存在

            if($res){
                if($res->password===md5($userInput['password'])){
                    $request->session()->put('userId', $res->id);
                    return redirect('index')->with('success','登陆成功');

                }else{

                    return redirect()->back()->with('error','密码错误');
                }
            }else{
                return redirect()->back()->with('error','账号不存在');
            }
        }
            return view('Auth\login');

    }
    public function update_password(Request $request){
        //获取session
        $userId = $request->session()->get('userId');
        $username = DB::table('users')->where('id','=',$userId)->value('username');
        if(!$userId){

            return redirect('/auth/login')->with('error','登录过期，请重新登录');
            exit;
        }

        if($request->isMethod('POST')){
            $userInput = $request->input();
            $password = $userInput['oldpwd'];

            if(md5($password)===DB::table('users')->where('id','=',$userId)->value('password')){
//                新密码
                $newpwd = $userInput['newpwd'];
                // 验证的正则
                $check_password  = "/^[A-Za-z]+[0-9]+[A-Za-z0-9]*|[0-9]+[A-Za-z]+[A-Za-z0-9]*$/";

                if (!preg_match($check_password, $newpwd)) {
                    return array('status' => 0, 'msg' => '密码必须由6-16个英文字母和数字的字符串组成！');
                    exit;
                }else{
                    //                执行修改操作
                    $res = DB::table('users') ->where('id', $userId) ->update(['password' => md5($newpwd),'updated_at'=>date("Y-m-d H:i:s",time())]);
                    if($res){

                        return array('status' => 1);
                    }else{
                        return array('status' => 0,'msg'=>'密码修改失败');
                    }
                }

            }else{
                return array('status' => 0, 'msg' => '原密码错误 请重新提交');
            }

        }

        return view('Auth\update_password',[
            'username'=>$username
        ]);
    }

    public function logout(Request $request){
        //清空session
        $request->session()->forget('userId');

        return redirect('/auth/login');
    }


}